added Feb 2001 SDK
[windows-sources.git] / shared source / sscli20 / tools / palsatellite / palsatellite.cpp
bloba7c53b5bfc1b4bba449e38bf031416412b1f26c2
1 // ==++==
2 //
3 //
4 // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
5 //
6 // The use and distribution terms for this software are contained in the file
7 // named license.txt, which can be found in the root of this distribution.
8 // By using this software in any fashion, you are agreeing to be bound by the
9 // terms of this license.
10 //
11 // You must not remove this notice, or any other, from this software.
12 //
13 //
14 // ==--==
16 // This tool scans through pal_error.h (must be in the current directory
17 // and finds all "#define ERROR_" lines. For each of those, it calls
18 // Win32 FormatMessageW to find the error text, and produces a
19 // rotor_pal.satellite file containing the error text. This satellite file
20 // can be used by the PAL on non-Win32 platforms.
22 #include <rotor_palrt.h>
24 #define DEFINE_PREFIX "#define "
26 #define STARTTAG "/* STARTERRORCODES" // comment
27 #define ENDTAG "/* ENDERRORCODES" // comment
29 char Line[4096];
30 WCHAR ErrorText[4096];
32 int __cdecl main(int argc, char *argv[]) {
33 FILE *fpIn;
34 FILE *fpOut;
35 int LineNum;
36 bool InErrorCodes;
38 fpIn = fopen("pal_error.h", "r");
39 if (!fpIn) {
40 fprintf(stderr,
41 "Unable to open pal_error.h for read\n");
42 return 1;
45 fpOut = fopen("rotor_pal.rc", "w");
46 if (!fpOut) {
47 fprintf(stderr,
48 "Unable to open rotor_pal.satellite for write\n");
49 return 1;
52 fprintf(fpOut, "// ==++==\n");
53 fprintf(fpOut, "//\n");
54 fprintf(fpOut, "// Copyright (c) Microsoft Corporation. All rights reserved.\n");
55 fprintf(fpOut, "//\n");
56 fprintf(fpOut, "// ==--==\n");
58 fprintf(fpOut, "\n// This file is autogenerated by the palsatellite tool from pal_error.h\n");
60 fprintf(fpOut, "\n#pragma code_page(65001)\n"); // UTF-8
62 fprintf(fpOut, "\nSTRINGTABLE\nBEGIN\n");
64 LineNum = 0;
65 InErrorCodes = false;
66 while (!feof(fpIn)) {
67 char *p;
68 int Value;
69 DWORD dw;
70 int i;
72 LineNum++;
73 fgets(Line, sizeof(Line), fpIn);
75 if (!InErrorCodes) {
76 if (strncmp(Line, STARTTAG, sizeof(STARTTAG)-1) == 0)
77 InErrorCodes = true;
79 else {
80 if (strncmp(Line, ENDTAG, sizeof(ENDTAG)-1) == 0)
81 InErrorCodes = false;
84 if (!InErrorCodes) {
85 // Line is not in the error codes block
86 continue;
89 if (strncmp(Line, DEFINE_PREFIX, sizeof(DEFINE_PREFIX)-1)) {
90 // Line doesn't start with "#define "
91 continue;
94 p = strchr(Line+sizeof(DEFINE_PREFIX), ' ');
95 if (!p) {
96 // Line isn't "#define xxxx yyyy"
97 continue;
100 Value = atoi(p+1);
101 if (Value < 0 || Value > 65536) {
102 fprintf(stderr,
103 "Win32 error value out of range at line %d\n", LineNum);
104 return 1;
107 dw = FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM|
108 FORMAT_MESSAGE_IGNORE_INSERTS,
109 NULL,
110 Value,
111 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
112 ErrorText,
113 sizeof(ErrorText)/sizeof(WCHAR),
114 NULL);
115 if (!dw) {
116 fprintf(stderr,
117 "FormatMessageW failed at line %d, Error=%d, LastError=%d\n",
118 LineNum,
119 Value,
120 GetLastError());
123 i = WideCharToMultiByte(CP_UTF8,
125 ErrorText,
127 Line,
128 sizeof(Line),
129 NULL,
130 NULL);
131 if (!i) {
132 fprintf(stderr,
133 "WCtoMB failed at line %d, LastError=%d\n",
134 LineNum,
135 GetLastError());
136 return 1;
139 fprintf(fpOut, "%d \"", Value);
141 p = Line;
142 while (*p) {
143 switch (*p) {
144 case 0xD:
145 // ignore Windows end of lines - the satellite file is meant to be used on Unix
146 break;
147 case 0xA:
148 fprintf(fpOut, "\\n");
149 break;
150 case '\"':
151 fprintf(fpOut, "\\\"");
152 break;
153 case '\\':
154 fprintf(fpOut, "\\\\");
155 break;
156 default:
157 fputc(*p, fpOut);
158 break;
160 p++;
163 fprintf(fpOut,"\"\n");
166 fprintf(fpOut, "END\n");
169 fclose(fpOut);
170 fclose(fpIn);
171 return 0;